package com.windgo.controller.wx;

import com.windgo.bean.MarketCart;
import com.windgo.bean.MarketUser;
import com.windgo.bean.bo.CartCheckedBO;
import com.windgo.bean.bo.CartCheckoutBO;
import com.windgo.bean.bo.CartDeleteBO;
import com.windgo.bean.bo.CartUpdateBO;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.vo.WxCartCheckoutVO;
import com.windgo.bean.vo.WxCartIndexVO;
import com.windgo.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * @author doublec
 * @since 2022/10/14 09:50
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;

    @RequestMapping("index")
    public BaseRespVo<WxCartIndexVO> cartIndex() {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        WxCartIndexVO wxCartIndexVO = wxCartService.indexCart(userId);
        return BaseRespVo.ok(wxCartIndexVO);
    }

    @RequestMapping("add")
    public BaseRespVo<Integer> cartAdd(@RequestBody MarketCart marketCart) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        int number = wxCartService.queryGoodsNumberById(marketCart.getGoodsId());
        if (marketCart.getNumber() <= number) {
            int count = wxCartService.addCart(userId, marketCart);
            return BaseRespVo.ok(count);
        }
        return BaseRespVo.ok(711, "库存不足");
    }


    @RequestMapping("fastadd")
    public BaseRespVo<Integer> cartFastAdd(@RequestBody MarketCart marketCart) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        int number = wxCartService.queryGoodsNumberById(marketCart.getGoodsId());
        if (marketCart.getNumber() <= number) {
            int cartId = wxCartService.fastAddCart(userId, marketCart);
            return BaseRespVo.ok(cartId);
        }
        return BaseRespVo.ok(711, "库存不足");
    }


    @RequestMapping("delete")
    public BaseRespVo<WxCartIndexVO> cartDelete(@RequestBody CartDeleteBO cartDeleteBO) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        WxCartIndexVO wxCartIndexVO = wxCartService.deleteCart(userId, cartDeleteBO);
        return BaseRespVo.ok(wxCartIndexVO);
    }

    @RequestMapping("checked")
    public BaseRespVo<WxCartIndexVO> cartChecked(@RequestBody CartCheckedBO cartCheckedBO) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        Integer isChecked = cartCheckedBO.getIsChecked();
//        Integer userId = 1;
        WxCartIndexVO wxCartIndexVO;
        if (isChecked == 0) {
            wxCartIndexVO = wxCartService.checkedCart(userId, cartCheckedBO, false);
        } else {
            wxCartIndexVO = wxCartService.checkedCart(userId, cartCheckedBO, true);
        }
        return BaseRespVo.ok(wxCartIndexVO);
    }


    @RequestMapping("update")
    public BaseRespVo cartUpdate(@RequestBody CartUpdateBO cartUpdateBO) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        int affectedRows = wxCartService.updateCart(userId, cartUpdateBO);
        return BaseRespVo.ok(0, "成功");
    }

    @RequestMapping("goodscount")
    public BaseRespVo<Integer> cartGoodsCount() {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        BaseRespVo baseRespVo;
        if (principals == null) {
            baseRespVo = BaseRespVo.ok(0);
        }
        else {
            MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
            Integer userId = primaryPrincipal.getId();
            Short totalNumber = wxCartService.goodsCountCart(userId);
            baseRespVo = BaseRespVo.ok(totalNumber);
        }
        return baseRespVo;

    }


    @RequestMapping("checkout")
    public BaseRespVo<WxCartCheckoutVO> cartCheckout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {
        CartCheckoutBO cartCheckoutBO = new CartCheckoutBO(cartId, addressId, couponId, userCouponId, grouponRulesId);
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        WxCartCheckoutVO wxCartCheckoutVO = wxCartService.checkoutCart(userId, cartCheckoutBO);
        return BaseRespVo.ok(wxCartCheckoutVO);
    }


}
